package online.szlib.com.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import online.szlib.com.core.domain.User;
import online.szlib.com.core.services.UserService;
import online.szlib.com.web.domain.UserBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    private static Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;
	
	@RequestMapping(value = "/saveUser", method = RequestMethod.POST)
	public ModelAndView saveUser(@ModelAttribute("command") UserBean userBean, 
			BindingResult result) {
		User user = prepareModel(userBean);
		userService.addUser(user);
		logger.info("Testing save user >>>>>>>>>>>>>>>>>>>>");
		return new ModelAndView("redirect:/addUser");
	}

	@RequestMapping(value="/users", method = RequestMethod.GET)
	public ModelAndView listUsers(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("users",  prepareListofBean(userService.listUsers()));
		return new ModelAndView("usersList", model);
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public ModelAndView addUser(@ModelAttribute("command")  UserBean userBean,
			HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("users",  prepareListofBean(userService.listUsers()));
		return new ModelAndView("addUser", model);
	}
	
	@RequestMapping(value = "/deleteUser", method = RequestMethod.GET)
	public ModelAndView editUser(@ModelAttribute("command")  UserBean userBean,
			BindingResult result) {
		userService.deleteUser(prepareModel(userBean));
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", null);
		model.put("users",  prepareListofBean(userService.listUsers()));
		return new ModelAndView("addUser", model);
	}
	
	@RequestMapping(value = "/editUser", method = RequestMethod.GET)
	public ModelAndView deleteUser(@ModelAttribute("command")  UserBean userBean,
			BindingResult result) {
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("user", prepareUserBean(userService.getUser(userBean.getId())));
		model.put("users",  prepareListofBean(userService.listUsers()));
		return new ModelAndView("addUser", model);
	}
	
	private User prepareModel(UserBean userBean){
		User user = new User();
		user.setAddress(userBean.getUsername());
		user.setUserName(userBean.getUsername());
		user.setPassword(userBean.getPassword());
		user.setUserId(userBean.getId());
		return user;
	}
	
	private List<UserBean> prepareListofBean(List<User> users){
		List<UserBean> beans = null;
		if(users != null && !users.isEmpty()){
			beans = new ArrayList<UserBean>();
			UserBean bean = null;
			for(User user : users){
				bean = new UserBean();
				bean.setUsername(user.getUserName());
				bean.setPassword(user.getPassword());
				bean.setAddress(user.getAddress());
				bean.setId(user.getUserId());
				beans.add(bean);
			}
		}
		return beans;
	}
	
	private UserBean prepareUserBean(User user){
		UserBean bean = new UserBean();
		bean.setAddress(user.getAddress());
		bean.setUsername(user.getUserName());
		bean.setPassword(user.getPassword());
		bean.setId(user.getUserId());
		return bean;
	}
}
